<?php

require_once 'Logger.php';
require_once 'RedisManager.php';
require_once 'Config.php';
require_once 'CardManager.php';
require_once 'GameCore.php';
require_once 'ScoreCalculator.php';
require_once 'PropSystem.php';

// 初始化游戏
$gameCore = new GameCore();
$gameId = 'game_' . uniqid();
$playerId = 'player_1';

// 开始新游戏
try {
    $gameState = $gameCore->startNewGame($gameId);
    echo "游戏开始！手牌数量: " . count($gameState['hand']) . "\n";
    echo "手牌信息: " . json_encode($gameState['hand'], JSON_UNESCAPED_UNICODE) . "\n";
} catch (Exception $e) {
    echo "游戏开始失败: " . $e->getMessage();
}


// 换牌示例
try {
    $gameState = $gameCore->discardCards($gameId, [0, 2, 4], [0, 1, 2]);
    echo "换牌成功！剩余换牌次数: " . $gameState['discard_chances'] . "\n";
    echo "换牌后手牌信息: " . json_encode($gameState['hand'], JSON_UNESCAPED_UNICODE) . "\n";
} catch (Exception $e) {
    echo "换牌失败: " . $e->getMessage() . "\n";
}

// 测试牌型
//try {
//    // 定义自定义牌组
//    $customCards = [
//        ["suit" => "梅花", "rank" => "5", "value" => 5, "id" => "梅花_5"],
//        ["suit" => "方片", "rank" => "6", "value" => 6, "id" => "方片_6"],
//        ["suit" => "梅花", "rank" => "6", "value" => 6, "id" => "梅花_6"],
//        ["suit" => "梅花", "rank" => "7", "value" => 7, "id" => "梅花_7"],
//        ["suit" => "方片", "rank" => "5", "value" => 5, "id" => "方片_5"],
//        ["suit" => "梅花", "rank" => "10", "value" => 10, "id" => "梅花_10"],
//        ["suit" => "梅花", "rank" => "8", "value" => 8, "id" => "梅花_8"],
//        ["suit" => "梅花", "rank" => "2", "value" => 2, "id" => "梅花_2"],
//        ["suit" => "红桃", "rank" => "5", "value" => 5, "id" => "红桃_5"],
//        ["suit" => "黑桃", "rank" => "5", "value" => 5, "id" => "黑桃_5"]
//    ];
//    // 调用方法创建测试数据
//    $gameCore->createCustomHandForTest($customCards, $gameId);
//    echo "创建测试数据成功";
//} catch (Exception $e) {
//    echo "创建测试数据失败: " . $e->getMessage();
//}

// 使用道具
try {
    // 使用道具
    $propSystem = new PropSystem();
    $propSystem->useProp($gameId, $playerId,'hand_multiplier', ['hand_type' => '对子']);

    $redisManager = new RedisManager();
    $inventory = $redisManager->getGameData($playerId, 'inventory') ?? [];
    echo "使用道具成功！剩余道具数量: " . json_encode($inventory, JSON_UNESCAPED_UNICODE) . "\n";

} catch (Exception $e) {
    echo "使用道具失败: " . $e->getMessage() . "\n";
}

// 出牌示例
try {
    $result = $gameCore->playCards($gameId, $playerId, [0, 1, 2]); // 出3张牌
    echo "牌型: " . $result['hand_type'] . "\n";
    echo "基础分: " . $result['base_score'] . "\n";
    echo "倍率: " . $result['multiplier'] . "\n";
    echo "总分: " . $result['total_score'] . "\n";
} catch (Exception $e) {
    echo "出牌失败: " . $e->getMessage() . "\n";
}

// 获取游戏状态
$currentState = $gameCore->getGameState($gameId);
echo "游戏状态: " . $currentState['status'] . "\n";
echo "剩余出牌数: " . $currentState['play_chances'] . "\n";
echo "换牌次数: " . $currentState['discard_chances'] . "\n";
echo "手牌数量: " . count($currentState['hand']);